package com.ling.sdk.live

import android.content.Intent
import android.support.annotation.CallSuper
import org.greenrobot.eventbus.EventBus

open class LiveUI : ILiveUI, ILiveUIParent, ILiveUIChild {


    override fun receiverMessage(any: Any): Boolean = false

    override fun onNetworkChange(isConnected: Boolean, netType: Int) {}

    var token = this.toString()

    @CallSuper
    override fun onAttachLive(token: String) {
        this.token = token
    }

    override fun getLiveToken() = token

    override fun onActivityResume() {}

    override fun onActivityPause() {}

    override fun onActivityStop() {}

    override fun onActivityFinish() {}

    override fun onUIActivityOnResult(requestCode: Int, result: Int, data: Intent?) {}

    override fun getContext() = LiveManager.getContext(token)

    override fun add(live: ILiveUI?) {
        LiveManager.addLiveUI(getContext(), live)
    }

    override fun remove(live: ILiveUI?) {
        LiveManager.removeLiveUI(getContext(), live)
    }

    override fun handler() = LiveManager.handler(getContext())

    override fun sendMessage(data: Any) = LiveManager.sendMessage(this, getContext(), data)

    override fun sendMessageAsync(data: Any) = LiveManager.sendMessageAsync(this, getContext(), data)


    fun exec(run: Runnable) {
        LiveManager.exec(getContext(), run)
    }


    fun postSticky(event: Any?) {
        EventBus.getDefault().postSticky(event)
    }


    fun post(event: Any?) {
        EventBus.getDefault().post(event)
    }

    @CallSuper
    override fun onCleanUp() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this)
    }

    fun enableEventBus() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this)
    }


}